<template>
  <div>
    <FilterTable
      ref="table"
      columnsRef="zjUploadFile"
      height="auto"
      :columns="columns"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
      :dataSource="list"
      :onSyncTableData="syncTableFun"
      :isPagination="false"
    >
      <template slot="controls" slot-scope="props">
<!--        <UploadFile action-url="/api/file/oss/upload" :initial-value="form.fileList" :isOnlyButton="true" :limit="10000" @change="successHandler">上传附件</UploadFile>-->
      </template>
    </FilterTable>
    <div style="height:60px"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
<!--      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">保 存</multiuse-button>-->
      <multiuse-button @click="closeDrawer(1)">关 闭</multiuse-button>
    </div>
  </div>
</template>
<script>
// import { checkExistsByOemId } from '@/api/technicalReportManagement/fileList.js';
import { queryByIdAndCode,saveOne} from '@/api/claim/dealer/ClaimApplication';
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
export default {
  data() {
    this.saveData = null;
    this.table = null;
    return {
      form: {
        fileList: []
      },
      list: [],
      syncList: [],
      columns: this.createdTable(),
      emitList: []
    };
  },
  mixins: [dictionary],
  props: ['rowData'],
  created() {
    console.log('编辑ID', this.rowData);
    // this.checkExistsByOemId(this.rowData);
  },
  async mounted() {
    this.table = this.$refs.table.$refs.pageTable;
    if(this.rowData.claimId){
      const res = await queryByIdAndCode({claimId:this.rowData.claimId,addCode :this.rowData.addCode })
      this.files = res.data;
      this.$refs.table.$refs.pageTable.EXECUTE_INSERT(res.data);
    }else{
      if(this.rowData.addFiles){
        this.$refs.table.$refs.pageTable.EXECUTE_INSERT(this.rowData.addFiles);
      }
    }

  },
  methods: {
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        // {
        //   title: '操作',
        //   dataIndex: 'column-action',
        //   width: 100,
        //   render: props => {
        //     return (
        //       <div style="text-align:center">
        //         <multiuse-button type="text" onClick={() => this.deleteFun(props.row)}>
        //           删除
        //         </multiuse-button>
        //       </div>
        //     );
        //   }
        // },
        {
          title: '附件标题',
          dataIndex: 'title',
          minWidth: 180,
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return (
              <a style="color:#bb0a30" href={p.row.fileUrl} target="_blank" download={p.row.title}>
                {p.row.title}
              </a>
            );
          }
        },
        // {
        //   title: '附件标题',
        //   dataIndex: 'title',
        //   showOverflowTooltip: true,
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   render: p => {
        //     return (
        //       <a style="color:#bb0a30" href={p.row.fileUrl} target="_blank" download={p.row.title}>
        //         {p.row.title}
        //       </a>
        //     );
        //   }
        // },
        {
          title: '上传时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    // async checkExistsByOemId(id) {
    //   let res = await checkExistsByOemId({ oemId: id });
    //   this.emitList = res.data || [];
    //   if (this.emitList.length > 0) {
    //     this.table.EXECUTE_INSERT(this.emitList);
    //   }
    // },
    syncTableFun(val) {
      this.syncList = val;
    },
    deleteFun(costItem) {
      this.$refs.table.$refs.pageTable.EXECUTE_DELETE([costItem]);
    },
    // 上传成功回调
    successHandler(val) {
      let json = JSON.parse(JSON.stringify(val));
      let data = {};
      data.createdAt = moment().format('YYYY-MM-DD HH:mm:ss');
      data.title = json[json.length - 1].name;
      data.fileUrl = json[json.length - 1].url;
      data.addCode = this.rowData.addCode;
      if(this.rowData.claimId){
        data.claimId = this.rowData.claimId;
      }
      this.table.EXECUTE_INSERT(data);
      // this.form.fileList.push(data);
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      }else{
        if(this.rowData.claimId){
          if(this.table.list.length>0){
            let res = await saveOne(this.table.list);
            if (res.resultCode === 200) {
              this.$notify.success({title: '提示信息', message: '保存成功'});
              this.$emit('close');
            }
          }else{
            this.files.map(x=>{
              delete x.fileUrl
            });
            let res = await saveOne( this.files);
            if (res.resultCode === 200) {
              this.$notify.success({title: '提示信息', message: '保存成功'});
              this.$emit('close');
            }
          }
        }else{
          this.table.list.addCode =  this.rowData.addCode;
          this.$emit('save',this.table.list);
        }
      }
    }
  }
};
</script>
